package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.PlatformService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.platform.vo.*;
import com.ibigcat.module.mdm.dao.entity.PlatformDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.PlatformMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 平台管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformServiceImpl implements PlatformService {

    @Resource
    private PlatformMapper platformMapper;

    @Override
    public Long createPlatform(PlatformSaveReqVO createReqVO) {
        // 插入
        PlatformDO platform = BeanUtils.toBean(createReqVO, PlatformDO.class);
        platformMapper.insert(platform);
        // 返回
        return platform.getId();
    }

    @Override
    public void updatePlatform(PlatformSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformExists(updateReqVO.getId());
        // 更新
        PlatformDO updateObj = BeanUtils.toBean(updateReqVO, PlatformDO.class);
        platformMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatform(Long id) {
        // 校验存在
        validatePlatformExists(id);
        // 删除
        platformMapper.deleteById(id);
    }

    private void validatePlatformExists(Long id) {
        if (platformMapper.selectById(id) == null) {
            throw exception(PLATFORM_NOT_EXISTS);
        }
    }

    @Override
    public PlatformDO getPlatform(Long id) {
        return platformMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformDO> getPlatformPage(PlatformPageReqVO pageReqVO) {
        return platformMapper.selectPage(pageReqVO);
    }

}